package foo.bar.wiki.web.action.page;

import foo.bar.wiki.web.action.BaseActionBean;
import foo.bar.wiki.web.extension.PageTypeConverter;
import foo.bar.wiki.services.WikiService;
import foo.bar.wiki.domain.Page;
import foo.bar.wiki.domain.Space;
import foo.bar.wiki.plugins.PluginException;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SimpleMessage;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.integration.spring.SpringBean;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class MarkPageAsSpaceDefaultActionBean extends BaseActionBean {


    @Validate(converter = PageTypeConverter.class)
    private Page page;


    public Resolution save_backToViewPage() throws PluginException {
        save();
        return new RedirectResolution(ViewPageActionBean.class, "view")
                .addParameter("currentPage", page.getId());
    }

    public Resolution save_backToEditPage() throws PluginException {
        save();
        return new RedirectResolution(EditPageActionBean.class, "view")
                .addParameter("currentPage", page.getId());
    }

    public Resolution save_backToWikiPage() throws PluginException {
        save();
        return new RedirectResolution(PageActionBean.class, "view")
                .addParameter("page", page.getId());
    }

    protected void save() throws PluginException {
        Space space = page.getSpace();
        wikiService.markPageAsSpaceDefault(page);
        getContext().getMessages().add(new SimpleMessage("Page ["+page.getName()+"] marked as default page for Space ["+space.getName()+"]"));
    }

    private WikiService wikiService;

    @SpringBean("wikiService")
    public void setWikiService(WikiService wikiService) {
        this.wikiService = wikiService;
    }


    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }
}
